# Copyright (C) 2007 "dbsolution sas" 
#
# This file is part of Weebotee.
#
# Weebotee is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Weebotee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Weebotee.  If not, see <http://www.gnu.org/licenses/>.

import datetime, random, sha
from django.db import models
from django.contrib.auth.models import User


UT = (("clients","Cliente"),
      ("commerciali", "Amministrativo"),
      ("developers", "Developer"))

TARGET_TYPE_CHOICES = (("mio_sito","Utilizzo per il mio Sito"), 
                   ("sito_clienti","Utilizzo per i miei Clienti"))

class Udetail(models.Model):
    key = models.CharField("Personal Key", max_length=50, editable=False, null=True)
    company = models.CharField(max_length=200, null=True)
    piva = models.CharField(max_length=16, help_text="partita iva o codice fiscale", null=True)
    address = models.CharField(max_length=200, null=True)
    cap = models.PositiveIntegerField(null=True)
    city = models.CharField(max_length=150, null=True)
    prov = models.CharField(max_length=2, null=True)
    tel = models.CharField(max_length=15, null=True)
    fax = models.CharField(max_length=15, null=True)
    website = models.URLField(null=True, help_text="http://")
    user_type = models.CharField(max_length=20, choices=UT)
    target_type = models.CharField(choices=TARGET_TYPE_CHOICES, blank=True, null=True, max_length=50)
    note = models.TextField(blank=True, null=True)
    
    # LDAP Related
    raw_passwd = None
    # Dipendenze
    user = models.ForeignKey(User, unique=True)

    def save(self):
        if not self.id:
            self.key = sha.new(str(random.random())).hexdigest()[:15] 
        super(Udetail, self).save()
        
    def __unicode__(self):
        return "%s (%s)" % (self.user, self.key)

    class Admin:
        pass
    
    class Meta:
        app_label='core'
